#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <errno.h>

#include "game_log.h"
#include "proto.h"
#include "room.h"
#include "player.h"
#include "game_server.h"

Player::Player() :
	AsyncTimer(YXJMJ_SERVER._base),
	_vip_level(0),
	_quick_hu_count(0)
{
	int ready_time = (YXJMJ_SERVER._game_flag / 1000000 == 3) ? 30 : 15;
	InitTimer(_ready_timer, ready_time, [this]()
	{
		GAME_LOG.debug("player[%d], ready_time_timeout.\n", _uid);
		if (YXJMJ_SERVER._game_flag / 1000000 == 3)
		{
			YXJMJ_SERVER._rooms[this->_roomid]->handler_player_ready(this, NULL);
			return;
		}
		YXJMJ_SERVER._rooms[this->_roomid]->del_player(this->_uid);
	});

	InitTimer(_pass_timer, 10, [this]()
	{
		GAME_LOG.debug("player[%d], do_pass_timer.\n", _uid);
		YXJMJ_SERVER._rooms[this->_roomid]->do_pass(this);
	});

	InitTimer(_auto_outmj_timer, 1, [this]() {
		GAME_LOG.debug("player[%d], do_auto_PUTOUT_MJ.\n", _uid);
		YXJMJ_SERVER._rooms[this->_roomid]->do_PUTOUT_MJ(this, this->_mo_mj.val);
	});

	InitTimer(_qiangganghu_timer, 10, [this]() {
		GAME_LOG.debug("player[%d], do_qiangganghu_timeout_pass.\n", _uid);
		YXJMJ_SERVER._rooms[this->_roomid]->do_qiangganghu_timeout_pass(this);
	});

	InitTimer(_getin_outmj_timer, 15, [this]() {
		GAME_LOG.debug("player[%d], getin_outmj_timer_cb.\n");
		//这个人超时了 变成机器人乱打牌
		this->_robot_op = 1;
		//通知所有人
		YXJMJ_SERVER._rooms[this->_roomid]->begin_robot_op_bc(this);
		YXJMJ_SERVER._rooms[this->_roomid]->do_PUTOUT_MJ(this, this->last_mj_val());
	});
}

Player::~Player()
{
	
}

int Player::init(int uid)
{
	_uid = uid;
	S_DB_PLAYER_INFO db_player_info;
	if (game_sql::get_db_player_info(db_player_info, _uid) < 0 )
	{
		GAME_LOG.error("player init error, get player[%d],mysql info error.\n", _uid);
		return -1;
	}
	
	if ( db_player_info.status == 0 )
	{
		GAME_LOG.info("player[%d], user invalid.\n", _uid);
		return -2;
	}
	
	_vip_level = game_sql::get_user_vip_level(_uid);
	_vip_level = (_vip_level == -1) ? 0 : _vip_level;

	_nick_name = db_player_info.nick_name;
	_login_ip = db_player_info.login_ip;
	_sex = db_player_info.sex;
	_avater_url = db_player_info.avater_url;
	_diamonds= db_player_info.diamonds;
	_gold = db_player_info.gold;
	_paiwei_score = db_player_info.paiwei_score;
	_robot_flag = db_player_info.robot_flag;
	_score = 0;

	_online = 1;
	//roomid = -1;//还没加入房间

	_zimo_times = 0;  
	_jiepao_times = 0; 
	_dianpao_times = 0; 
	_anggang_times = 0; 
	_minggang_times = 0; 
	_vec_score.clear();
	
	reset();
	
	return 0;
}

void Player::reset()
{
	_status = NOT_READY;
	
	_hole_mjs.clear();
	_form_mjs.clear();
	_out_mjs.clear();
	
	_cur_gang_score = 0;         //总杠分
	_cur_angang_score = 0;       //暗杠score
	_cur_minggang_score = 0;     //明杠score
	_cur_fanggang_score = 0;     //放杠score
	_cur_pao_flag = 0;
	_cur_end_flag = 0;	
	
	_card_type = CARD_TYPE_ERROR;    
	_jiaopai_flag = 0;
	_dq_val = 0;
	
	_fangpaiji_num = 0;
	_gudingji_num_out = 0;
	_gudingji_num_hole = 0;
	_chongfengji_num = 0; //冲锋鸡 算分鸡数

	_fangpaiji_score = 0;
	_gudingji_score = 0;
	_chongfengji_score = 0; //冲锋鸡得分
	_zherenji_score = 0;    //责任鸡得分

	_cardtype_score = 0;
	_winlose_score = 0;
		
	_auto_outmj_timer.stop();
	_pass_timer.stop();

	_agree_dissroom = 0;
	_have_gang = 0;
	_tian_hu_flag = 0;
	_di_hu_flag = 0;

	_mo_cnt = 0;
	_cur_baoting_flag = 0;	
	_guo_shou_bu_hu_flag = 0;
	_robot_op = 0;
}


int Player::get_info()
{
	return 0;
}

bool Player::do_out_mj(Mj mj)
{
	remove_n_mj(_hole_mjs.mjs, mj, 1);
	_out_mjs.add_mj(mj);
	return true;
}


int Player::calculate_fangpaiji_base_num(Mj fangpai_mj)
{
	if ( _jiaopai_flag == 0 )    //未叫牌，不计算翻牌鸡
	{
		return 0;
	}

	Mj fangpaiji_mj = fangpai_mj.next_fangpai_mj();
	
	int num1 = get_one_mj_num(_hole_mjs.mjs, fangpaiji_mj);
	int num2 = _form_mjs.get_one_mj_num(fangpaiji_mj);
	int num3 = get_one_mj_num(_out_mjs.mjs, fangpaiji_mj);

	if (fangpaiji_mj.val == S1) //是一条, 翻牌鸡为金鸡
	{
		return (num1+num2+num3)*2;
	}
	else
	{
		return num1+num2+num3;	
	}
}

int Player::calculate_gudingji_base_num_hole()
{
	Mj gudingji_mj;
	gudingji_mj.val = S1;
	int num1 = get_one_mj_num(_hole_mjs.mjs, gudingji_mj);
	int num2 = _form_mjs.get_one_mj_num(gudingji_mj);

	int all_num = num1+num2;
	if (YXJMJ_SERVER._rooms[this->_roomid]->_zherenji_to_player == this) //有责任鸡
	{
		
		all_num = all_num - YXJMJ_SERVER._rooms[this->_roomid]->_zherenji_type;
	}
	
	if (_jiaopai_flag == 1)
	{
		if (YXJMJ_SERVER._rooms[this->_roomid]->_fangpaiji_mj.val == S1) //翻牌鸡是一条, 所有鸡加倍
		{
			return all_num *2;	
		}
		else
		{
			return all_num;
		}
	}
	else
	{
		return 0;
	}
}

int Player::calculate_gudingji_base_num_out()
{
	Mj gudingji_mj;
	gudingji_mj.val = S1;
	int num1 = get_one_mj_num(_out_mjs.mjs, gudingji_mj);

	int all_num = num1;
	if (YXJMJ_SERVER._rooms[this->_roomid]->_chongfengji_player == this) //有冲峰鸡，则必有一个打出去的固定鸡算去冲锋鸡了
	{
		all_num = all_num - 1;
	}
	
	if (_jiaopai_flag == 1)
	{
		if (YXJMJ_SERVER._rooms[this->_roomid]->_fangpaiji_mj.val == S1) //翻牌鸡是一条, 所有鸡加倍
		{
			return all_num *2;
		}
		else
		{
			return all_num;
		}
	}
	else
	{
		if (YXJMJ_SERVER._rooms[this->_roomid]->_fangpaiji_mj.val == S1) //翻牌鸡是一条, 所有鸡加倍
		{
			return -all_num *2;
		}
		else
		{
			return -all_num;
		}
	}
}


int Player::calculate_chongfengji_base()
{
	int val = 0;
	if (YXJMJ_SERVER._rooms[this->_roomid]->_chongfengji_player == this ) //有冲锋鸡
	{
		val = 1;
		if (YXJMJ_SERVER._rooms[this->_roomid]->_fangpaiji_mj.val == S1) //翻牌鸡是一条, 所有鸡加倍
		{
			val = 2;
		}
	}

	if (_jiaopai_flag == 1)
	{
		return val;
	}
	else
	{
		return -val;
	}
}

int Player::last_mj_val()
{
	if (!_hole_mjs.mjs.empty())
	{
		return _hole_mjs.mjs.rbegin()->val;
	}
	return 0;
}



